security: harden JSON parsing in shell scripts#104
Merged
Conversation
Greptile SummaryThis PR replaces brittle Key improvements:
Files updated:
Security context: This hardens parsing in security-critical deployment and audit scripts, reducing risk from malformed metadata files. Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Shell Script Needs JSON Value] --> B{jq available?}
B -->|Yes| C[jq -er with filter]
B -->|No| D{python3 available?}
D -->|Yes| E[Python json.load]
D -->|No| F[Return Error Code 2]
C --> G{Parse Success?}
E --> G
G -->|Value Found & String| H[Return 0 + Print Value]
G -->|Key Missing/Wrong Type| I[Return 1]
G -->|JSON/File Error| F
H --> J[Scripts Use Value]
I --> K[Scripts Handle Missing Key]
F --> L[Scripts Handle Parse Error]
style H fill:#90EE90
style I fill:#FFD700
style F fill:#FFB6C6
Last reviewed commit: ebcbf49 |
Comment on lines
+304
to
+307
| short="$(sudo -u "$agent_user" sh -c "cat '$version_file' 2>/dev/null" | json_get_string_stdin "short" 2>/dev/null || true)" | ||
| sha="$(sudo -u "$agent_user" sh -c "cat '$version_file' 2>/dev/null" | json_get_string_stdin "sha" 2>/dev/null || true)" | ||
| branch="$(sudo -u "$agent_user" sh -c "cat '$version_file' 2>/dev/null" | json_get_string_stdin "branch" 2>/dev/null || true)" | ||
| deployed_at="$(sudo -u "$agent_user" sh -c "cat '$version_file' 2>/dev/null" | json_get_string_stdin "deployed_at" 2>/dev/null || true)" |
There was a problem hiding this comment.
Running cat 4 separate times via sudo. Consider reading the file once and piping to 4 separate json_get_string_stdin calls, or creating a temp variable to avoid multiple process spawns.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI
This is a comment left during a code review.
Path: bin/baudbot
Line: 304-307
Comment:
Running `cat` 4 separate times via sudo. Consider reading the file once and piping to 4 separate `json_get_string_stdin` calls, or creating a temp variable to avoid multiple process spawns.
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
bin/lib/json-common.shwith shared JSON extraction helpers (jqfast-path,python3fallback)grep|sedJSON parsing in critical shell scripts (bin/baudbot,bin/deploy.sh,bin/update-release.sh,bin/rollback-release.sh,bin/security-audit.sh)bin/lib/json-common.test.shand run them viatest/shell-scripts.test.mjsValidation
Notes